<?php
/**
 *  店铺后台设备账号的管理
 * Author: lyun
 * Date: 2018/4/8
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Store;

use App\Http\Controllers\ApiController;
use App\Http\Middleware\StoreAuth;
use App\Http\Requests\Admin\Customer\Device\DeviceAccountPatchEnabledRequest;
use App\Http\Requests\Admin\Customer\DeviceAccountIndexRequest;
use App\Http\Requests\Admin\Store\DeviceAccountUpdateRequest;
use App\Models\Admin\StoreAdmin;
use App\Models\Customer\DeviceAccount;
use App\Models\Panx\SkinDetector;
use App\Services\Utils\Guard;
use Illuminate\Http\Request;

class DeviceAccountController extends ApiController
{
    const ROUTES = [
        'index'  => 'api.admin.store.device.account.index',
        'update' => 'api.admin.store.device.account.update',
        'patch'  => 'api.admin.store.device.account.patch',
    ];

    /**
     * Display a listing of the resource.
     * @test
     *
     * @param DeviceAccountIndexRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(DeviceAccountIndexRequest $request)
    {
        // 超管才能操作的
        $this->checkIsTrue(StoreAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        $device_accounts = DeviceAccount::where('store_id', Guard::store()->id)
            ->columnsKeywordsOrLike([
                'device_name' => $request->keyword,
                'account' => $request->keyword,
            ])
            ->latest()
            ->paginate($request->per_page ?? 15);

        return $this->response(compact('device_accounts'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $skin_detectors = SkinDetector::all();

        return $this->response(compact('skin_detectors'));
    }
    
    /**
     * Update the specified resource in storage.
     * @test
     *
     * @param  DeviceAccountUpdateRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(DeviceAccountUpdateRequest $request, $id)
    {
        // 超管才能操作的
        $this->checkIsTrue(StoreAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var DeviceAccount $device_account */
        $device_account = DeviceAccount::where('store_id', Guard::store()->id)
            ->find($id);
        $this->checkItemExist($device_account, trans('admin.device_account'));

        $device_account_data = $this->assembleDeviceAccount($request);

        $updated = $device_account->update($device_account_data);

        return $this->response(compact('updated', 'device_account'));
    }

    /**
     * 启用和禁用设备账号
     * @test
     *
     * @param DeviceAccountPatchEnabledRequest $request
     * @param $id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function patchEnabled(DeviceAccountPatchEnabledRequest $request,
                                 $id)
    {
        $storeAdmin = Guard::storeAdmin();

        // 超管才能操作的
        $this->checkIsTrue($storeAdmin->isSuperMan(), trans('admin.is_super_man'));

        /** @var DeviceAccount $device_account */
        $device_account = DeviceAccount::where('store_id', $storeAdmin->store_id)
            ->find($id);
        $this->checkIsTrue($device_account, trans('admin.device_account'));

        $updated = $device_account->update([
            'enabled' => $request->enabled,
//            'uuid'    => null
        ]);

        return $this->response(compact('updated', 'device_account'));
    }

    /**
     * @param DeviceAccountUpdateRequest $request
     * @return array
     */
    private function assembleDeviceAccount(DeviceAccountUpdateRequest $request)
    {
        $data = [
            'account'          => $request->account,
        ];

        if ($request->device_name) {
            $data['device_name'] = $request->device_name;
        }

        if ($request->skin_detector_id) {
            $data['skin_detector_id'] = $request->skin_detector_id;
        }

        if ($request->password) {
            $data['password'] = $request->password;
            $data['password_modified_at'] = now();
        }

        return $data;
    }
}
